#include <stdio.h>
#include <string.h>
#include "student.h"

// 打印菜单的函数
void print_menu(){
	printf("--------------------------\n");
	printf("| 1.添加  2.修改  3.删除 |\n");
	printf("| 4.查找  5.排序  6.退出 |\n");
	printf("--------------------------\n");
	printf("请输入你的选择:");
}

// 添加学员信息的函数
int insert_student(class_t *my_class){
	if(N == my_class->count){
		printf("班级人数已经满 添加失败\n");
		return -1;
	}
	printf("请输入新学员的信息(学号 姓名 成绩):");
#if 0
	int input_id = 0;
	char input_name[32] = {0};
	int input_score = 0;
	scanf("%d%s%d", &input_id, input_name, &input_score);

	my_class->person[my_class->count].id = input_id;
	strcpy(my_class->person[my_class->count].name, input_name);
	my_class->person[my_class->count].score = input_score;
#else
	scanf("%d%s%d", &(my_class->person[my_class->count].id),\
				my_class->person[my_class->count].name,\
				&(my_class->person[my_class->count].score));
#endif
	// 判断学号是否重复
	int i = 0;
	for(i = 0; i < my_class->count; i++){
		if(my_class->person[i].id == my_class->person[my_class->count].id){
			printf("输入的学号冲突 插入失败\n");
			return -1;
		}
	}
	my_class->count++;
	printf("学员信息插入成功\n");
	return 0;
}

// 打印所有学员信息的函数--实际不需要 就是学习阶段方便看现象的
void print_student(class_t *my_class){
	printf("\n");
	int i = 0;
	for(i = 0; i < my_class->count; i++){
		printf("%-5d%-10s%-3d\n", \
				my_class->person[i].id,\
				my_class->person[i].name,\
				my_class->person[i].score);
	}
	printf("\n");
	return;
}

// 修改学员信息的函数--根据学号修改名字和成绩
int modify_student(class_t *my_class){
	printf("请输入要修改的学员的学号:");
	int input_id = 0;
	scanf("%d", &input_id);
	// 判断是否有要找的学员
	int i = 0;
	for(i = 0; i < my_class->count; i++){
		if(my_class->person[i].id == input_id){
			break;
		}
	}
	if(i < my_class->count){
		// 输出新的名字和成绩
		printf("请输入新的姓名和成绩:");
		scanf("%s%d", my_class->person[i].name, &(my_class->person[i].score));
		printf("学员信息修改成功\n");
		return 0;
	}else{
		printf("查无此人 修改失败\n");
		return -1;
	}
}

// 查找学员信息的函数--根据姓名查找
int search_student(class_t *my_class){
	printf("请输入要查找的学员的姓名:");
	char input_name[32] = {0};
	scanf("%s", input_name);
	// 遍历数组
	int i = 0;
	for(i = 0; i < my_class->count; i++){
		if(!strcmp(my_class->person[i].name, input_name)){
			printf("找到学员[%d %s %d]\n",\
					my_class->person[i].id,\
					my_class->person[i].name,\
					my_class->person[i].score);
			return 0;
		}
	}
	printf("查无此人\n");
	return -1;
}

// 删除学员信息的函数--根据学号删除
int delete_student(class_t *my_class){
	printf("请输入要删除的学员的学号:");
	int input_id = 0;
	scanf("%d", &input_id);
	// 遍历数组 确定要删除的学员的位置
	int i = 0;
	int j = 0;
	for(i = 0; i < my_class->count; i++){
		if(my_class->person[i].id == input_id){
			// 执行删除操作
			for(j = i+1; j < my_class->count; j++){
				my_class->person[j-1] = my_class->person[j];
			}
			my_class->count--;
			printf("学员信息删除成功\n");
			return 0;
		}
	}
	printf("查无此人 删除失败\n");
	return -1;
}

// 学员信息排序--根据成绩降序排序
int sort_student(class_t *my_class){
	int i = 0;
	int j = 0;
	stu_t temp;
	for(i = 0; i < my_class->count-1; i++){
		for(j = 0; j < my_class->count-1-i; j++){
			if(my_class->person[j].score < my_class->person[j+1].score){
				temp = my_class->person[j];
				my_class->person[j] = my_class->person[j+1];
				my_class->person[j+1] = temp;
			}
		}
	}
	return 0;
}
